package com.itheima.pay.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("pay")
public class PayController {


    /**
     * 预下单：返回二维码的链接，二维码链接生成二维码图片
     * https://openapi.alipay.com/gateway.do   生产环境地址
     * https://openapi.alipaydev.com/gateway.do  沙箱环境地址
     */
    @GetMapping("precreate/{orderNo}")
    public void precreate(@PathVariable Long orderNo) throws Exception {
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",
                "2016100200645088",
                "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCN7OAjILDnmYUc8N6TOWWZUEnR9lCdSsVrjQHIZ6JbyF+IQJ2hhSFYmsE3pwzCVn3KsIXqmuN+cEKkYQV+LN63voY+CFLuSVA9ECwgUKn/yUNajURi59ljSusHE9jYP42MXM7p+9ayHCv8PKXmYn5zqh8eAfzWhMT4sTWPMOXio8rgu+gV4dScYRCZL+COyRVjhU1FgsZTalAhAwlzHsLgz+ykL/yu0w7fB6D4+sNIJkvytDacDkYQTyBwxF3SFukr9PakeSSSNh/cbPnexBPz4BxtSHDEz4Icyzf4olVCyZP9sr8xFKrK9nh6o7IVwrzzyNGoKUEyaaV1d2wfLG3XAgMBAAECggEAaLJnnkJNfmx4ECN4bi8XTgQ2kNceHQq9nFuHBym8Uez83GyeXDrzoIN5Ke7nsK1RWqbiA3GDhRCXeN/Bx5SeL/oy2awWUYZv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m8IOP1GBwh8hJGnUYiUOoUPf2214ZEpHCf4/IuFTGnmxyaKsDw==",
                "json",
                "GBK",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl1NG6w+hLEUwQvXViCSZJFoFY6vdkpK9/PRo7bBSo2JV1TOqHeAGBF0cGlH/GA16sPpXgE+hYIRv0fQLMmQxE54GHGhHCp2qXvqNdcOeUuazGkV70FBIXl0CUdgVkpuVhNCTa4llooCJsFGftD/BtcyW65TcY4GAU5Km+ZoLkgRQIV+cEBWgo4yHdQpJPYDZmMmG5rbY7YsL/gSPsAL1NwjdOH4lBv+IpTWGUJJ5KtLcN7d9F5a2KdEZTuN1JqeROgJp32q2hbpUeat4X/1S1xgpmZ1BJ7LgSALcn9bLmbXJLGc6wZ1sB4kp/TCgrkANJpzOPp7XMGjBXiZ4v6Yl9QIDAQAB",
                "RSA2");
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setNotifyUrl("");   // 支付结果：回调支付宝-->餐掌柜    主动调用API查询
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", orderNo); //交易单号：20210817010101003
        bizContent.put("total_amount", 100000); // 单位元
        bizContent.put("subject", "华为 P999 256G");

        request.setBizContent(bizContent.toString());  // JSON 字符串
        AlipayTradePrecreateResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
            System.out.println(response.getQrCode());
        } else {
            System.out.println("调用失败");
        }
    }


    /**
     * 查询支付结果
     */
    @GetMapping("query/{orderNo}")
    public AlipayTradeQueryResponse queryOrder(@PathVariable Long orderNo) throws Exception {

        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",
                "2016100200645088",
                "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCN7OAjILDnmYUc8N6TOWWZUEnR9lCdSsVrjQHIZ6JbyF+IQJ2hhSFYmsE3pwzCVn3KsIXqmuN+cEKkYQV+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/YH3Kz2bPl0B5SifHbsgX/sbiTRl97a/DcU4UnFUB2PeeR9bIdju3JW36hqmftf1RAoGAf3wu+jbsmD/AfgYP7GazJHHUdAztdVXlEQbVxYuErzqgeu0KGFB+5tBtxGhVXU6QhTZkrHOh5XllL7Yfu3kb+QVqJv7O8wiFtI3qo+JoH12iXjHYUMS1HabwoNjmjeWuA1w1u07DVbGdXme7e4iXgoMYgYJ+yB9tpcxPvu5lyzECgYBs1CfrL59mCV8PRNsdSeIB7bjfzm3rrBFMl5eWlSPOFzRlxZRBIdFbwE43gKs18CIN/XuS/heDxbhZdPIRCU40lV8EnSdj6qTvC+5H14Os/DNyf9YgetyBBV6Bm8IOP1GBwh8hJGnUYiUOoUPf2214ZEpHCf4/IuFTGnmxyaKsDw==",
                "json",
                "GBK",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl1NG6w+hLEUwQvXViCSZJFoFY6vdkpK9/PRo7bBSo2JV1TOqHeAGBF0cGlH/GA16sPpXgE+hYIRv0fQLMmQxE54GHGhHCp2qXvqNdcOeUuazGkV70FBIXl0CUdgVkpuVhNCTa4llooCJsFGftD/BtcyW65TcY4GAU5Km+ZoLkgRQIV+cEBWgo4yHdQpJPYDZmMmG5rbY7YsL/gSPsAL1NwjdOH4lBv+IpTWGUJJ5KtLcN7d9F5a2KdEZTuN1JqeROgJp32q2hbpUeat4X/1S1xgpmZ1BJ7LgSALcn9bLmbXJLGc6wZ1sB4kp/TCgrkANJpzOPp7XMGjBXiZ4v6Yl9QIDAQAB",
                "RSA2");

        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();

        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", orderNo); //交易单号：20210817010101003

        request.setBizContent(bizContent.toString());  // JSON 字符串
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
            String tradeStatus = response.getTradeStatus();
            if ("TRADE_SUCCESS".equals(tradeStatus)) {
                System.out.println("支付成功...");
                System.out.println("处理业务逻辑....");
            }
            return response;
        } else {
            System.out.println("调用失败");
        }

        return null;
    }


    @GetMapping("refund/{orderNo}/{money}/{refundNo}")
    public AlipayTradeRefundResponse refund(@PathVariable Long orderNo ,@PathVariable String money,@PathVariable Long refundNo)  throws  Exception{
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",
                "2016100200645088",
                "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCN7OAjILDnmYUc8N6TOWWZUEnR9lCdSsVrjQHIZ6JbyF+IQJ2hhSFYmsE3pwzCVn3KsIXqmuN+cEKkYQV+LN63voY+CFLuSVA9ECwgUKn/yUNajURi59ljSusHE9jYP42MXM7p+9ayHCv8PKXmYn5zqh8eAfzWhMT4sTWPMOXio8rgu+gV4dScYRCZL+COyRVjhU1FgsZTalAhAwlzHsLgz+ykL/yu0w7fB6D4+sNIJkvytDacDkYQTyBwxF3SFukr9PakeSSSNh/cbPnexBPz4BxtSHDEz4Icyzf4olVCyZP9sr8xFKrK9nh6o7IVwrzzyNGoKUEyaaV1d2wfLG3XAgMBAAECggEAaLJnnkJNfmx4ECN4bi8XTgQ2kNceHQq9nFuHBym8Uez83GyeXDrzoIN5Ke7nsK1RWqbiA3GDhRCXeN/Bx5SeL/oy2awWUYZv1LwLcUNQRUtZjoFMA5J5gZAQ1UXE37HyX6tPULjhZr71i7vJvqzGD9ld3yskM7P6AmPn2Nv/mjKTj38oIx4QUPaOWMMXdBeqHmpmdKOOvI7kPdw6yXvk17ZoV5sWUou0ghsJmI3EsMjsW/MWyXNSFe5P55KAa/qsvIEKfh+brYmHwUyOtENq3iT6QFMGNNhJqVYVhgCRt3f0bpz6blLgaa8aKZI68nKU+XABieYN3q58v7gLSFmUwQKBgQDTM1iyoKeuqVVlHmDjNWO/A6oFiv8bYP1Bud3QsdlZOdPlxp/J0DwX/GOtA+3Gow/70bmV45O02fpr3uTRbaNLDwOj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IpTWGUJJ5KtLcN7d9F5a2KdEZTuN1JqeROgJp32q2hbpUeat4X/1S1xgpmZ1BJ7LgSALcn9bLmbXJLGc6wZ1sB4kp/TCgrkANJpzOPp7XMGjBXiZ4v6Yl9QIDAQAB",
                "RSA2");

        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();

        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", orderNo); //交易单号：20210817010101003
        bizContent.put("refund_amount",money ); //退款金额
        bizContent.put("out_request_no",refundNo ); //部分退款
        request.setBizContent(bizContent.toString());  // JSON 字符串
        AlipayTradeRefundResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
            return response;
        } else {
            System.out.println("调用失败");
        }

        return null;
    }

}
